﻿cmake_minimum_required(VERSION 3.0)
cmake_policy(SET CMP0048 NEW)
#定义工程名及版本
project(teapoy VERSION 3.0.0)
set(soversion 3.0)

# 包含公共代码*
if(NOT SUBMODULE)
	set(SUBMODULE ${PROJECT_SOURCE_DIR}/3rd_party)
endif()
include(${SUBMODULE}/libmilk/libmilk/libmilk.cmake)

# 设置环境变量*
libmilk_cmake_init_env()

# 搜索代码
unset(codes)
unset(headers)

aux_source_directory(src codes)
aux_source_directory(src/sni codes)
file(GLOB headers RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} include/teapoy/*.h)


#搜索子模块
set(CMAKE_CXX_STANDARD 11)

if(NOT TARGET milk)
	if(shared)
		add_subdirectory(${SUBMODULE}/libmilk/libmilk ${SUBMODULE_BIN}/libmilk)
	else()
		add_subdirectory(${SUBMODULE}/libmilk/libmilk ${SUBMODULE_BIN}/libmilk EXCLUDE_FROM_ALL)
	endif()
endif()

if(NOT TARGET cavedb)
	if(shared)
		add_subdirectory(${SUBMODULE}/cavedb/cavedb ${SUBMODULE_BIN}/cavedb)
	else()
		add_subdirectory(${SUBMODULE}/cavedb/cavedb ${SUBMODULE_BIN}/cavedb EXCLUDE_FROM_ALL)
	endif()
endif()


#搜索依赖
if(UNIX)
	pkg_check_modules(OPENSSL openssl)
	pkg_check_modules(LIBCURL libcurl)
	pkg_check_modules(PCRE libpcre REQUIRED)
	pkg_check_modules(ZLIB zlib)
	pkg_check_modules(NGHTTP2 libnghttp2)
	pkg_check_modules(JS17 mozjs-17.0 REQUIRED)
	pkg_check_modules(TINYXML2 tinyxml2)

	pkg_check_modules(MYSQL mysqlclient)
	check_include_file_cxx(jpeglib.h Z_HAVE_LIBJPEG)
	check_include_file_cxx(mongo/version.h Z_HAVE_MONGODB)
	check_include_file_cxx(mongo/client/connpool.h Z_HAVE_LIBMONGO)
endif()

if(OPENSSL_FOUND)
	list(APPEND BUILD_DEPENDS openssl-devel)
	list(APPEND RUN_DEPENDS openssl)
	list(APPEND LIBS_DEPENDS ${OPENSSL_LDFLAGS})
	list(APPEND INCS_DEPENDS ${OPENSSL_CFLAGS})
	list(APPEND CONFIG_MACROS OPENSSL_FOUND)
else()
	# 没有处理切换协议方法，http情况下是无法切换到http2的。只在https情况下使用http2
	list(REMOVE_ITEM codes src/http_2_0_nghttp2.cpp)
	list(REMOVE_ITEM codes src/http_2_0_nghttp2.h)
endif()

if(LIBCURL_FOUND)
	list(APPEND BUILD_DEPENDS libcurl-devel)
	list(APPEND RUN_DEPENDS libcurl)
	list(APPEND LIBS_DEPENDS ${LIBCURL_LDFLAGS})
	list(APPEND INCS_DEPENDS ${LIBCURL_CFLAGS})
	list(APPEND CONFIG_MACROS LIBCURL_FOUND)
else()
	list(REMOVE_ITEM codes src/sni/curl.cpp)
endif()

if(JS17_FOUND)
	list(APPEND BUILD_DEPENDS mozjs17-devel)
	list(APPEND RUN_DEPENDS mozjs17)
	list(APPEND LIBS_DEPENDS ${JS17_LDFLAGS})
	list(APPEND INCS_DEPENDS ${JS17_CFLAGS})
	list(APPEND CONFIG_MACROS JS17_FOUND)
else()
	list(REMOVE_ITEM codes src/js_extend17.cpp)
endif()

if(TINYXML2_FOUND)
	list(APPEND BUILD_DEPENDS tinyxml2-devel)
	list(APPEND RUN_DEPENDS tinyxml2)
	list(APPEND LIBS_DEPENDS ${TINYXML2_LDFLAGS})
	list(APPEND INCS_DEPENDS ${TINYXML2_CFLAGS})
	list(APPEND CONFIG_MACROS TINYXML2_FOUND)
else()
	list(REMOVE_ITEM codes src/sni/xml.cpp)
endif()

if(MYSQL_FOUND)
	list(APPEND BUILD_DEPENDS libmysqlclient-dev)
	list(APPEND RUN_DEPENDS libmysqlclient)
	list(APPEND LIBS_DEPENDS ${MYSQL_LDFLAGS})
	list(APPEND CONFIG_MACROS MYSQL_FOUND)
else()
	list(REMOVE_ITEM codes src/sni/mysql.cpp)
	list(REMOVE_ITEM codes src/functional_impl_mysql.cpp)
endif()

if(PCRE_FOUND)
	list(APPEND BUILD_DEPENDS pcre-devel)
	list(APPEND RUN_DEPENDS pcre)
	list(APPEND LIBS_DEPENDS ${PCRE_LDFLAGS})
	list(APPEND INCS_DEPENDS ${PCRE_CFLAGS})
	list(APPEND CONFIG_MACROS PCRE_FOUND)
endif()

if(ZLIB_FOUND)
	list(APPEND BUILD_DEPENDS zlib-devel)
	list(APPEND RUN_DEPENDS zlib)
	list(APPEND LIBS_DEPENDS ${ZLIB_LDFLAGS})
	list(APPEND INCS_DEPENDS ${ZLIB_CFLAGS})
	list(APPEND CONFIG_MACROS ZLIB_FOUND)
endif()

if(NGHTTP2_FOUND)
	list(APPEND BUILD_DEPENDS libnghttp2-devel)
	list(APPEND RUN_DEPENDS libnghttp2)
	list(APPEND LIBS_DEPENDS ${NGHTTP2_LDFLAGS})
	list(APPEND INCS_DEPENDS ${NGHTTP2_CFLAGS})
	list(APPEND CONFIG_MACROS NGHTTP2_FOUND)
	list(REMOVE_ITEM codes src/http_2_0_hpack_huffman.cpp)
	list(REMOVE_ITEM codes src/http_2_0_hpack.cpp)
	list(REMOVE_ITEM codes src/http_2_0_hpack.cpp)
else()
	list(REMOVE_ITEM codes src/http_2_0_nghttp2.cpp)
	list(REMOVE_ITEM codes src/http_2_0_nghttp2.h)
endif()

if(Z_HAVE_LIBJPEG)
	list(APPEND BUILD_DEPENDS libjpeg-turbo-devel)
	list(APPEND RUN_DEPENDS libjpeg-turbo)
	list(APPEND LIBS_DEPENDS -ljpeg)
	list(APPEND CONFIG_MACROS Z_HAVE_LIBJPEG)
else()
	list(REMOVE_ITEM codes src/sni/jpeg.cpp)
endif()

if(Z_HAVE_MONGODB)
	list(APPEND BUILD_DEPENDS mongo-cxx-driver-devel)
	list(APPEND RUN_DEPENDS mongo-cxx-driver)
	list(APPEND LIBS_DEPENDS -lmongoclient)
	list(APPEND CONFIG_MACROS Z_HAVE_MONGODB)
elseif(Z_HAVE_LIBMONGO)
	list(APPEND BUILD_DEPENDS libmongodb-devel)
	list(APPEND RUN_DEPENDS libmongodb)
	list(APPEND LIBS_DEPENDS -lmongoclient)
	list(APPEND CONFIG_MACROS Z_HAVE_LIBMONGO)
else()
	list(REMOVE_ITEM codes src/sni/mongo.cpp)
endif()


#定义目标*
libmilk_cmake_def(${PROJECT_NAME} ${PROJECT_NAME}-shared teapoy codes headers ${PROJECT_NAME}-alone)
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} include/teapoy)

if(${PROJECT_NAME}-shared)
	target_link_libraries(${PROJECT_NAME} PUBLIC milk)
	target_link_libraries(${PROJECT_NAME} PUBLIC cavedb)
	target_link_libraries(${PROJECT_NAME} PRIVATE leveldb)
else()	
	target_link_libraries(${PROJECT_NAME} PUBLIC milk)
	target_link_libraries(${PROJECT_NAME} PUBLIC cavedb)
	target_link_libraries(${PROJECT_NAME} PRIVATE leveldb)
endif()

#定义主程序
add_executable(teapoyd src/app/teapoyd.cc)
install(TARGETS teapoyd DESTINATION bin)

target_link_libraries(teapoyd PUBLIC -Wl,--whole-archive milk cavedb ${PROJECT_NAME} -Wl,--no-whole-archive ${LIBS_DEPENDS})
#target_link_libraries(teapoyd ${PROJECT_NAME} ${LIBS_DEPENDS} -ltinyxml2)
